package com.guo.pet_store.admin.service;

import com.guo.pet_store.admin.dao.PetRepository;
import com.guo.pet_store.admin.pojo.Pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class PetServiceImpl implements PetService{
    @Autowired
    PetRepository petRepository;
    @Override
    public Page<Pet> listPets(Pageable pageable) {
        return petRepository.findAll(pageable);
    }

    @Override
    public Pet addPet(Pet pet) {

        return petRepository.save(pet);
    }

    @Override
    public Pet getOnePet(Long id) {
        return petRepository.findPetById(id);
    }
    @Override
    public List<Pet> listPets() {
        return petRepository.findAll();
    }

    @Override
    public List<Pet> getPetsByType(Long id) {
        return petRepository.findPetTypes(id);
    }

    @Override
    public void deleteAPet(Long id) {

        Date now=new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        String dateAndTime=dateFormat.format(now);
        petRepository.deletePet(id,dateAndTime);
    }

    @Override
    public List<Pet> listPetsOnSale() {
        return petRepository.selectPetsOnSale();
    }

    @Override
    public List<Pet> searchPets(String search,String likeSearch) {
        return petRepository.searchPet(search,likeSearch);
    }

//    根据父type查询所有宠物
    @Override
    public List<Pet> petsByParentType(Long parentTypeId) {
        return petRepository.petsByParentId(parentTypeId);
    }

    @Override
    public void setCount(int pet_id, long shopCarId,int count) {
        petRepository.setCount(pet_id,shopCarId,count);
    }
}
